package com.chaoyi.service.Impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.chaoyi.dao.mysql.RoleMapper;
import com.chaoyi.model.Role;
import com.chaoyi.service.RoleService;

@Service
public class RoleServiceImpl implements RoleService {
	@Resource
	private RoleMapper roleMapper;

	@Override
	public List<Role> findRoleList(String offset, String limit) {
		return roleMapper.findRoleList(offset,limit);
	}

	@Override
	public int addRole(String roleName) {
		return roleMapper.addRole(roleName);
	}

	@Override
	public int deleteRole(int id) {
		//先删除角色菜单关联表的数据
		roleMapper.deleteModelInfo(id);
		return roleMapper.deleteRole(id);
	}

	@Override
	public List<Role> findModelInfo(int roleId) {
		return roleMapper.findModelInfo(roleId);
	}

	@Override
	public int updateModelInfo(int roleId, List<String> menuInfoList) {
		int i =0;
		try {
			if(menuInfoList.size()!=0){
				//先删除表里的数据 要不保存不进去
				roleMapper.deleteModelInfo(roleId);
				List<Role> roleList = new ArrayList<Role>();
				for(String temp : menuInfoList){
					Role role = new Role();
					role.setRoleId(roleId);
					role.setModelId(Integer.parseInt(temp));
					roleList.add(role);
				}
				roleMapper.updateModelInfo(roleList);
				i=1;
			}
		} catch (Exception e) {
			i =0;
		}
		return i;
	}

	@Override
	public List<Role> findRoleName() {
		return roleMapper.findRoleName();
	}
}
